package com.javaspeak.algorithm.deletedirectory.nonrecursive;

import java.io.File;
import java.util.ArrayDeque;
import java.util.Deque;

/**
 * Delete Files in Directory Non Recursively
 *
 * @author John Dickerson
 */
public class DeleteFiles {

    private static void processStack(
            Deque<File> fileStack, Deque<File> directoryStack){

        File file = fileStack.pop();

        if ( file.isFile() ){
            file.delete();
        }
        else {

            directoryStack.add( file );

            File[] children = file.listFiles();

            for ( File child: children ){

                fileStack.add( child );
            }
        }
    }

    public static void deleteFiles( File file ){

        Deque<File> fileStack = new ArrayDeque<File>();

        Deque<File> directoryStack = new ArrayDeque<File>();

        fileStack.add( file );

        while ( fileStack.size() != 0 ){

            processStack( fileStack, directoryStack );
        }

        while ( directoryStack.size() != 0 ){

            directoryStack.pop().delete();
        }
    }
}
